package com.pks.insurance.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.pks.insurance.domain.User;
import com.pks.insurance.service.UserManager;

/**
 * User related operations Servlet implementation class Used to update user
 * profile
 * 
 * @author Pankaj Soni
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(UserServlet.class);
	private UserManager userManager;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOGGER.debug("begin");
		String task = request.getParameter("task");
		LOGGER.info("task: " + task);
		ApplicationContext context = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		userManager = (UserManager) context.getBean("userManager");
		if ("profile-update".equals(task)) {
			String ssn = (String) request.getSession(false).getAttribute("ssn");
			User user = userManager.getUser(ssn);
			request.setAttribute("customer", user);
			request.getRequestDispatcher("profileUpdate.jsp").forward(request,
					response);
			return;
		} else {
			LOGGER.debug("not relavent task");
		}
		LOGGER.debug("end");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LOGGER.debug("begin");
		String task = request.getParameter("task");
		LOGGER.info("task: " + task);
		ApplicationContext context = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		userManager = (UserManager) context.getBean("userManager");
		if ("profile-update".equals(task)) {
			this.userProfileUpdate(request, response);
		}
		LOGGER.debug("end");
	}

	private void userProfileUpdate(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			LOGGER.debug("begin");
			String ssn = request.getParameter("ssn");
			String passwd = request.getParameter("passwd");
			String firstName = request.getParameter("fName");
			String lastName = request.getParameter("lName");
			String gender = request.getParameter("gender");
			String mobileNo = request.getParameter("mobileNo");
			String emailId = request.getParameter("email");
			String city = request.getParameter("city");
			String bloodGroup = request.getParameter("bloodGroup");
			String drivingLicence = request.getParameter("drivingLicence");
			String dateOfBirthString = request.getParameter("dateOfBirth");
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-dd");
			java.util.Date dateOfBirth = null;
			try {
				dateOfBirth = dateFormat.parse(dateOfBirthString);
			} catch (ParseException e) {
				LOGGER.error(e.getMessage());
			}

			User user = new User();
			user.setSsn(ssn);
			user.setPasswd(passwd);
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setGender(gender);
			user.setDateOfBirth(dateOfBirth);
			user.setMobileNo(mobileNo);
			user.setEmail(emailId);
			user.setCity(city);
			user.setBloodGroup(bloodGroup);
			user.setDrivingLicence(drivingLicence);
			LOGGER.info("user: " + user);
			userManager.updateUser(user);
			request.getRequestDispatcher("home.jsp").forward(request, response);
		} catch (ServletException e) {
			LOGGER.error(e.getMessage());
		} catch (IOException e) {
			LOGGER.error(e.getMessage());
		}
		LOGGER.debug("end");

	}

}
